package czx.model;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MappedSuperclass;

/**
 * 抽取操作用户信息作为基类，方便其他表继承
 * 注意： 需要在类名前添加基类标识 @MappedSuperclass
 * 
 * @author 陈正兴
 * 
 */
@MappedSuperclass
public class OperateUser {

	private Tuser tuserByCreateuserid;
	private Date createtime;
	private String createusername;
	private Tuser tuserByModifyuserid;
	private Date modifytime;
	private String modifyusername;

	public OperateUser() {
		Date dt = new Date();
		this.createtime = dt;
		this.modifytime = dt;
		this.createusername = "";
		this.modifyusername = "";
	}

	/**
	 * 创建用户
	 */
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "CREATEUSERID", nullable = false)
	public Tuser getTuserByCreateuserid() {
		return tuserByCreateuserid;
	}

	public void setTuserByCreateuserid(Tuser tuserByCreateuserid) {
		this.tuserByCreateuserid = tuserByCreateuserid;
	}

	/**
	 * 创建时间
	 */
	@Column(name = "CREATETIME", nullable = false)
	public Date getCreatetime() {
		return createtime;
	}

	public void setCreatetime(Date createtime) {
		this.createtime = createtime;
	}

	/**
	 * 创建人用户名
	 */
	@Column(name = "CREATEUSERNAME", nullable = false, length = 100)
	public String getCreateusername() {
		return createusername;
	}

	public void setCreateusername(String createusername) {
		this.createusername = createusername;
	}

	/**
	 * 最后修改用户
	 */
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "MODIFYUSERID", nullable = false)
	public Tuser getTuserByModifyuserid() {
		return tuserByModifyuserid;
	}

	public void setTuserByModifyuserid(Tuser tuserByModifyuserid) {
		this.tuserByModifyuserid = tuserByModifyuserid;
	}

	/**
	 * 最后修改时间
	 */
	@Column(name = "MODIFYTIME", nullable = false)
	public Date getModifytime() {
		return modifytime;
	}

	public void setModifytime(Date modifytime) {
		this.modifytime = modifytime;
		/**
		 * 由于Hibernate 是通过setter/getter方法对对象的属性进行赋值,如果自定义一个setter方法，如：
		 * this.modifytime = new Date();
		 * 可能造成了缓存中的持久化对象与数据库对象的不一 致，Hibernate 会以为该持久化对象已被改变
		 * 那么就会造成select 后自动生成一条 update 命令
		 */
	}

	/**
	 * 最后修改人的用户名
	 */
	@Column(name = "MODIFYUSERNAME", nullable = false, length = 100)
	public String getModifyusername() {
		return modifyusername;
	}

	public void setModifyusername(String modifyusername) {
		this.modifyusername = modifyusername;
	}

	public void Validate() throws ValidateException {
		if (this.tuserByCreateuserid == null)
			throw new ValidateException("创建用户不能为空！");
		if (this.tuserByModifyuserid == null)
			throw new ValidateException("修改用户不能为空！");
	}

	public void updateCreateUser(Tuser t) {
		if (t != null) {
			this.tuserByCreateuserid = t;
			this.createusername = t.getChname();
		}
	}

	public void updateModifyUser(Tuser t) {
		if (t != null) {
			this.tuserByModifyuserid = t;
			this.modifyusername = t.getChname();
			this.modifytime = new Date();
		}
	}
}
